package net.sf.cldsql.sql2json.sql;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;

import net.sf.cldsql.dj.utl.StrUtl;


/**
 * JsonBuilder
 */
public class JsonBuilder {
    
    private static JsonBuilder instance = new JsonBuilder();

    public static JsonBuilder get() {
        return instance;
    }
    
    public StringBuilder convResultSetToJson(ResultSet rset) {
        return convResultSetToJson(rset, new StringBuilder("  "));
    }

    public StringBuilder convResultSetToJson(ResultSet rset, StringBuilder intent) {
        final StringBuilder buf = new StringBuilder();
        try {
            
            ResultSetMetaData meta = rset.getMetaData();
            
            ColumnMeta[] dict = new ColumnMeta[meta.getColumnCount()];
            for (int j = 0; j < meta.getColumnCount(); j ++) {
                dict[j] = new ColumnMeta(meta.getColumnName(j + 1), meta.getColumnType(j + 1));
            }
            
            buf.append("{\"rset\":[\n");
            int i;
            for (i = 0; rset.next(); i ++) {
                if (i != 0) {
                    buf.append(",\n");
                }
                StringBuilder intentInner = new StringBuilder(intent).append("  ");
                buf.append(intentInner);
                buf.append("{");
                for (int j = 0; j < dict.length; j ++) {
                    switch (dict[j].getColumnType()) {
                    case Types.BOOLEAN:
                        break;
                    case Types.BIT:
                        break;
                    case Types.TINYINT:
                        break;
                    case Types.BIGINT:
                        break;
                    case Types.INTEGER:
                        break;
                    case Types.NUMERIC:
                        break;
                    case Types.DECIMAL:
                        break;
                    case Types.DATE:
                        break;
                    case Types.DOUBLE:
                        break;
                    case Types.FLOAT:
                        break;
                    case Types.REAL:
                        break;
                    case Types.JAVA_OBJECT:
                        break;
                    case Types.LONGNVARCHAR:
                        break;
                    case Types.LONGVARBINARY:
                        break;
                    case Types.LONGVARCHAR:
                        break;
                    case Types.CHAR:
                        break;
                    case Types.NCHAR:
                        break;
                    case Types.VARCHAR:
                        break;
                    case Types.NVARCHAR:
                        break;
                    case Types.CLOB:
                        break;
                    case Types.NCLOB:
                        break;
                    case Types.SMALLINT:
                        break;
                    case Types.TIME:
                        break;
                    case Types.TIMESTAMP:
                        break;
                    case Types.BINARY:
                        break;
                    case Types.VARBINARY:
                        break;
                    case Types.NULL:
                        break;
                    case Types.ARRAY:
                        break;
                    case Types.BLOB:
                        break;
                    case Types.DATALINK:
                        break;
                    case Types.DISTINCT:
                        break;
                    case Types.SQLXML:
                        break;
                    case Types.STRUCT:
                        break;
                    case Types.REF:
                        break;
                    case Types.ROWID:
                        break;
                    case Types.OTHER:
                        break;
                    default:
                        
                    }
                    if (j != 0) {
                        buf.append(", ");
                    }
                    buf.append('"');
                    buf.append(StrUtl.get().convSqlToLJava(dict[j].getColumnName()));
                    buf.append('"');
                    buf.append(':');
                    buf.append('"');
                    buf.append(rset.getString(j + 1));
                    buf.append('"');
                }
                buf.append("}");
            }
            buf.append("\n  ],\n" +
                    "  \"status\":\"0\",\n" +
                    "  \"count\":\"" + i + "\"\n" +
                            "}");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            
        }
        return buf;
        //return new StringBuilder("[{\"rset\":{\"name\":\"test\"}}]");
    }

}
